package org.example.huawei;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: lynn
 * @Descript: 三个线程依次打印，或者三个线程同时打印
 * @date: 2023/7/22 23:37
 * @version: 1.0
 */
public class SwaggerThreeThread {
    private static final char[] au="ABCDE".toCharArray();
    private static final char[] bu="abcde".toCharArray();
    private static final char[] cu="12345".toCharArray();

    public static void main(String[] args) {
        SwaggerThreeThread stt=new SwaggerThreeThread();
        stt.ThreeThreadPrintInSameTime();


        stt.ThreeThreadJTPrint();

        stt.ThreeThreadYCPrint();


    }




    /**
     * 三个线程同时打印
     */
    public void ThreeThreadPrintInSameTime(){
        ExecutorService executorService= Executors.newCachedThreadPool();
        CountDownLatch countDownLatch=new CountDownLatch(1);

        executorService.submit(()->{
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread A execute time: "+System.currentTimeMillis());

        });
        executorService.submit(()->{
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread B execute time: "+System.currentTimeMillis());

        });
        executorService.submit(()->{
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread C execute time: "+System.currentTimeMillis());

        });

        countDownLatch.countDown();

        executorService.shutdown();
    }

    /**
     * 交替打印
     */
    public  void  ThreeThreadJTPrint(){
        Lock lock=new ReentrantLock();
        Condition c1=lock.newCondition();
        Condition c2=lock.newCondition();
        Condition c3=lock.newCondition();

        Thread t1=new Thread(()->{

            try {
                lock.lock();
                for (char c:au){
                    System.out.println(c);

                        c2.signal();
                        c1.await();

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }


        });

        Thread t2=new Thread(()->{
            try {
                lock.lock();
                for (char c:bu){
                    System.out.println(c);

                        c3.signal();
                        c2.await();

                }
            } catch (InterruptedException e) {

            } finally {
                lock.unlock();
            }

        });

        Thread t3=new Thread(()->{

            try {
                lock.lock();
                for (char c:cu){
                    System.out.println(c);

                        c1.signal();
                        c3.await();

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        });

        t1.start();
        t2.start();
        t3.start();

    }

    /**
     * 依次打印
     */
    public  void  ThreeThreadYCPrint(){


    }
}
